/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Contendor;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.impl.TorneoServiceImpl;
import entity.Torneo;
import service.impl.ContendorServiceImpl;

@Controller
public class TorneoController {
    
    private TorneoServiceImpl TorneoServiceImpl;

    public void setTorneoServiceImpl(TorneoServiceImpl TorneoServiceImpl) {
        this.TorneoServiceImpl = TorneoServiceImpl;
    }
    
    private ContendorServiceImpl ContendorServiceImpl;

    public void setContendorServiceImpl(ContendorServiceImpl ContendorServiceImpl) {
        this.ContendorServiceImpl = ContendorServiceImpl;
    }
    
    @RequestMapping(params="accion=qry")
    public ModelAndView query(){
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneo");
        mav.addObject("list",list);
        Torneo Torneo = new Torneo();
        mav.addObject("Torneo",Torneo);
        return mav;
    }
    
    @RequestMapping(params="accion=qryParticipante")
    public ModelAndView queryParticipante(){
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneoParticipante");
        mav.addObject("list",list);
        Torneo Torneo = new Torneo();
        mav.addObject("Torneo",Torneo);
        return mav;
    }
    
    @RequestMapping(params="accion=qryReporte")
    public ModelAndView queryReporte(){
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneoReporte");
        mav.addObject("list",list);
        Torneo Torneo = new Torneo();
        mav.addObject("Torneo",Torneo);
        return mav;
    }

    
    @RequestMapping(params="accion=qryAsignar")
    public ModelAndView queryAsignar(HttpServletRequest request){
        String id = request.getParameter("id");
        Torneo Torneo = null;
        if(id != null){
            Torneo = TorneoServiceImpl.buscarPorId(Double.valueOf(id));
        }
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("AsignarContendor");
        mav.addObject("list",list);
        mav.addObject("Torneo",Torneo);
        return mav;
    }
    
    @RequestMapping(params="accion=fnd")
    public ModelAndView find(@ModelAttribute Torneo Torneo){
        List<Torneo> list = TorneoServiceImpl.buscar(Torneo);
        ModelAndView mav = new ModelAndView("ListarTorneo");
        mav.addObject("list",list);
        Torneo Torneo1 = new Torneo();
        mav.addObject("Torneo",Torneo1);
        return mav;
    }
    
    
    @RequestMapping(params="accion=ins1")
    public ModelAndView insert1(){
        ModelAndView mav = new ModelAndView("IngresarTorneo");
        Torneo Torneo = new Torneo();
        mav.addObject("Torneo",Torneo);
        return mav;
    }
    
    @RequestMapping(params="accion=ins2")
    public ModelAndView insert2(@ModelAttribute Torneo Torneo){
        Integer result = TorneoServiceImpl.insertar(Torneo);
        result = 0;
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneo");
        mav.addObject("list",list);
        Torneo Torneos = new Torneo();
        mav.addObject("Torneo",Torneos);
        return mav;
    }
    
    @RequestMapping(params="accion=asignar")
    public ModelAndView asignar(@ModelAttribute Torneo Torneo,HttpServletRequest request){
        String[] id2 = request.getParameterValues("chkDel");
        Integer result = 0;
        Integer conteo = 0;
        Integer parejas = id2.length/2;
        for (int i = 0; i < id2.length; i++) {
            if (conteo==2){
                parejas = parejas - 1;
                conteo = 0;
            }
            conteo = conteo + 1;
            String codigo = id2[i];
            result = TorneoServiceImpl.asignar(Torneo,Double.parseDouble(codigo),parejas);
        }
        result = 0;
        Double idTorneo = Torneo.getCodtor();
        Torneo Torneo1 = null;
        if(idTorneo != null){
            Torneo1 = TorneoServiceImpl.buscarPorId(idTorneo);
        }
        List<Contendor> list = ContendorServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("AsignarContendor");
        mav.addObject("list",list);
        mav.addObject("Torneo",Torneo1);
        return mav;
    }
    
    @RequestMapping(params="accion=del")
    public ModelAndView delete(HttpServletRequest request){
        String id = request.getParameter("id");
        if(id!=null){
            TorneoServiceImpl.eliminar(Double.valueOf(id));
        }
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneo");
        mav.addObject("list",list);
        Torneo Torneos = new Torneo();
        mav.addObject("Torneo",Torneos);
        return mav;
    }
    
    @RequestMapping(params="accion=lanzar")
    public ModelAndView lanzar(HttpServletRequest request){
        String id = request.getParameter("id");
        if(id!=null){
            TorneoServiceImpl.lanzar(Double.valueOf(id));
        }
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneoParticipante");
        mav.addObject("list",list);
        Torneo Torneos = new Torneo();
        mav.addObject("Torneo",Torneos);
        return mav;
    }
    
    @RequestMapping(params="accion=upd1")
    public ModelAndView update1(HttpServletRequest request){
        String id = request.getParameter("id");
        Torneo Torneo = null;
        if(id != null){
            Torneo = TorneoServiceImpl.buscarPorId(Double.valueOf(id));
        }
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("EditarTorneo");
        mav.addObject("list",list);
        mav.addObject("Torneo",Torneo);
        return mav;
    }
    
    @RequestMapping(params="accion=upd2")
    public ModelAndView update2(@ModelAttribute Torneo Torneo){
        TorneoServiceImpl.actualizar(Torneo);
        List<Torneo> list = TorneoServiceImpl.buscarTodos();
        ModelAndView mav = new ModelAndView("ListarTorneo");
        mav.addObject("list",list);
        Torneo Torneos = new Torneo();
        mav.addObject("Torneo",Torneos);
        return mav;
    }
}
